<?php

$secret = $_GET['secret'];

class mycrypt {

    public $pubkey;
    public $privkey;

    function __construct() {
        $this->pubkey = file_get_contents('./key/public.key');
        $this->privkey = file_get_contents('./key/private.key');
    }

    public function encrypt($data) {
        if (openssl_public_encrypt($data, $encrypted, $this->pubkey))
            $data = base64_encode($encrypted);
        else
            throw new Exception('Unable to encrypt data. Perhaps it is bigger than the key size?');

        return $data;
    }

    public function decrypt($data) {
        if (openssl_private_decrypt(base64_decode($data), $decrypted, $this->privkey))
            $data = $decrypted;
        else
            $data = '';

        return $data;
    }
    /*1024长度的RSA加密最多只能加密长度为117的字符串。*/
    function encrypt_data($data)
    {
        $crypt_res = "";
        for($i=0;$i<((strlen($data) - strlen($data)%117)/117+1); $i++)
        {
            $crypt_res = $crypt_res.($this->encrypt(mb_strcut($data, $i*117, 117, 'utf-8')));
        }
        return $crypt_res;
    }
    function decrypt_data($data)
    {
        $decrypt_res = "";
        $datas = explode('=',$data);
        foreach ($datas as $value)
        {
            $decrypt_res = $decrypt_res.$this-> decrypt($value);
        }
        return $decrypt_res;
    }


}

header("Content-type: application/javascript; charset=utf-8");
$callback = $_GET['callback'];

$mycrypt = new mycrypt();
$data =  $mycrypt->decrypt_data($secret);
$data = json_decode($data,true);

$response = array(
    'status'=>0,
    'msg'=>'',
    'data'=>$data,
);
$response = json_encode($response);
echo $callback."(".$response.")";
 